<template>
  <!-- 表格 -->
  <ele-pro-table
    ref="tableRef"
    row-key="id"
    :columns="columns"
    :datasource="datasource"
    height="calc(100vh - 290px)"
    tool-class="ele-toolbar-form"
    :scroll="{ x: 800 }"
    tools-theme="default"
    bordered
    cache-key="contentCategpryArticle"
    class="content-article-table"
  >
    <template #toolbar>
      <cate-article-search @search="reload" @add="openEdit()" />
    </template>
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'type'">
        <a-tag color="blue" v-if="record.type === 0">文章</a-tag>
        <a-tag color="red" v-if="record.type === 1">外链</a-tag>
        <a-tag color="green" v-if="record.type === 2">图集</a-tag>
      </template>
      <template v-else-if="column.key === 'status'">
        <a-switch
          :checked="record.status === 0"
          @change="(checked: boolean) => editStatus(checked, record)"
        />
      </template>
      <template v-else-if="column.key === 'action'">
        <a-space>
          <a @click="openEdit(record)">修改</a>
          <a-divider type="vertical" />
          <a-popconfirm
            placement="topRight"
            title="确定要删除此内容吗？"
            @confirm="remove(record)"
          >
            <a class="ele-text-danger">删除</a>
          </a-popconfirm>
        </a-space>
      </template>
    </template>
  </ele-pro-table>
  <!-- 编辑弹窗 -->
  <cate-article-edit
    :data="current"
    v-model:visible="showEdit"
    :cate-list="cateList"
    :cate-id="cateId"
    @done="reload"
  />
</template>

<script lang="ts" setup>
  import { ref, watch } from 'vue';
  import { message } from 'ant-design-vue/es';
  import type { EleProTable } from 'ele-admin-pro/es';
  import type {
    DatasourceFunction,
    ColumnItem
  } from 'ele-admin-pro/es/ele-pro-table/types';
  import { messageLoading, toDateString } from 'ele-admin-pro/es';
  import CateArticleSearch from './cate-article-search.vue';
  import CateArticleEdit from './cate-article-edit.vue';
  import type { Category } from '@/api/content/category/model';
  import { Article, ArticleParam } from '@/api/content/article/model';
  import {
    pageArticles,
    removeArticle,
    updateArticleStatus
  } from '@/api/content/article';

  const props = defineProps<{
    // 栏目 id
    cateId?: number;
    // 全部栏目
    cateList: Category[];
  }>();

  // 表格实例
  const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);

  // 表格列配置
  const columns = ref<ColumnItem[]>([
    {
      key: 'index',
      width: 48,
      align: 'center',
      fixed: 'left',
      hideInSetting: true,
      customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
    },
    {
      title: 'ID',
      width: 60,
      align: 'center',
      dataIndex: 'id',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '标题',
      dataIndex: 'title',
      sorter: true,
      ellipsis: true,
      showSorterTooltip: false
    },
    {
      title: '作者',
      dataIndex: 'user',
      width: 80,
      align: 'center',
      sorter: true,
      showSorterTooltip: false
    },
    {
      title: '类型',
      width: 80,
      dataIndex: 'type',
      key: 'type'
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      sorter: true,
      showSorterTooltip: false,
      ellipsis: true,
      customRender: ({ text }) => toDateString(text)
    },
    {
      title: '状态',
      key: 'status',
      showSorterTooltip: false,
      width: 80,
      align: 'center'
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      align: 'center'
    }
  ]);

  // 当前编辑数据
  const current = ref<Article | null>(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 表格数据源
  const datasource: DatasourceFunction = ({ page, limit, where, orders }) => {
    return pageArticles({
      ...where,
      ...orders,
      page,
      limit,
      cateId: props.cateId
    });
  };

  /* 搜索 */
  const reload = (where?: ArticleParam) => {
    tableRef?.value?.reload({ page: 1, where });
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: Article) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /* 删除单个 */
  const remove = (row: Article) => {
    const hide = messageLoading('请求中..', 0);
    removeArticle(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 修改用户状态 */
  const editStatus = (checked: boolean, row: Article) => {
    const status = checked ? 0 : 1;
    updateArticleStatus(row.id, status)
      .then((msg) => {
        row.status = status;
        message.success(msg);
      })
      .catch((e) => {
        message.error(e.message);
      });
  };

  // 监听 id 变化
  watch(
    () => props.cateId,
    () => {
      reload();
    }
  );
</script>

<style lang="less" scoped>
  .content-article-table :deep(.ant-table-body) {
    overflow: auto !important;
    overflow: overlay !important;
  }

  .content-article-table :deep(.ant-table-pagination.ant-pagination) {
    padding: 0 4px;
    margin-bottom: 0;
  }
</style>
